package core.command;

import java.util.ArrayList;

import core.AlignedRectangle2D;
import core.Drawing;
import core.Point2D;
import core.Stroke;
import core.UserContext;

public class SelectionCommand extends AbstractCommand {

	ArrayList<Stroke> selectedStrokesBefore = new ArrayList<Stroke>();
	ArrayList<Stroke> selectedStrokesAfter = new ArrayList<Stroke>();
	private UserContext context;
	
	public SelectionCommand(Drawing drawing, ArrayList< Point2D > lassoPolygonPoints, UserContext context) {
		super(drawing);
		this.context = context;
		this.selectedStrokesBefore = (ArrayList<Stroke>) context.getSelectedStrokes().clone();
		
		for ( Stroke s : drawing.strokes ) {
			if ( s.isContainedInLassoPolygon( lassoPolygonPoints ) )
				selectedStrokesAfter.add( s );
		}
	}
	
	public SelectionCommand(Drawing drawing, AlignedRectangle2D selectedRectangle, UserContext context) {
		super(drawing);
		this.context = context;
		this.selectedStrokesBefore = (ArrayList<Stroke>) context.getSelectedStrokes().clone();
		
		for ( Stroke s : drawing.strokes ) {
			if ( s.isContainedInRectangle( selectedRectangle ) )
				selectedStrokesAfter.add( s );
		}
	}

	@Override
	public void execute() {
		this.context.setSelectedStrokes((ArrayList<Stroke>) selectedStrokesAfter.clone());
	}

	@Override
	public void undo() {
		this.context.setSelectedStrokes((ArrayList<Stroke>) selectedStrokesBefore.clone());
	}
}